package com.example.zselasticsearchsf.controller;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

/**
 * @Author: Steven
 * @Date: 2021/10/19 5:43
 */

public class movie {

    public void  aa () throws IOException {
        WebClient web = new WebClient(BrowserVersion.FIREFOX_45);
 /*
            long startTime = System.currentTimeMillis();    //获取开始时间
            web.getOptions().setJavaScriptEnabled(true); //启用JS解释器，默认为true
            web.setJavaScriptTimeout(20000);//设置JS执行的超时时间
            web.getOptions().setThrowExceptionOnFailingStatusCode(false);//当HTTP的状态非200时是否抛出异常, 这里选择不需要
            web.getOptions().setActiveXNative(false);
            web.getOptions().setCssEnabled(false); //禁用css支持
            web.addRequestHeader("User-Agent", " Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36");
            web.getOptions().setThrowExceptionOnScriptError(false); //js运行错误时，是否抛出异常
            web.getOptions().setTimeout(10000); //设置连接超时时间 ，这里是10S。如果为0，则无限期等待
            web.setAjaxController(new NicelyResynchronizingAjaxController());//设置支持AJAX
            HtmlPage htmlpage = web.getPage("");//爬取url地址
            HtmlForm from = htmlpage.getElementByName("loginForm");
            HtmlInput loginId = from.getInputByName("username");//获取用户名输入框
            HtmlInput pwd = from.getInputByName("password");//获取密码输入框
            loginId.setValueAttribute(number);//用户名
            pwd.setValueAttribute(password);//密码
            HtmlPage hhtmlPage = from.getInputByName("submitBtn").click();//模拟点击登录
            DataInfo dataInfo = new DataInfo();
            //以下代码为了判断点击登录后页面还未加载出来，进行重试操作，最大重试次数5次
            int s = 0;
            while (hhtmlPage.getByXPath("//div[@class='personal-list']/a").size() < 1) {
                if (s > 5) {
                    dataInfo.setSuccess("time");
                    return dataInfo;
                }
                if (hhtmlPage.asXml().contains("密码错误") || hhtmlPage.asXml().contains("账户不存在")) {
                    dataInfo.setSuccess("password");
                    return dataInfo;
                }
                Thread.sleep(500);
                System.out.println("----------------------等待尝试重新点击");
                hhtmlPage = from.getInputByName("submitBtn").click();//模拟点击登录
                s++;
            }
           HtmlPage htmlpage2 = web.getPage("");//登录成功进入下一个页面，获取信息
            String str = htmlpage2.asXml();
            Document doc = Jsoup.parse(str);
            Elements tr = doc.getElementById("studentInfoTb").select("tr");
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setNumber(tr.get(1).select("td").get(1).text());//学号
            studentInfo.setName(tr.get(1).select("td").get(3).text());//姓名

        tr.get(1).select("td").get(4).select("img").attr("src");//头像地址
        saveImage( web.getPage(url).getWebResponse().getContentAsStream(),
                "网络路径","本地保存路径")
*/


    }



    public static String saveImage(InputStream inputStream, String savePath, String filePath) {
        byte[] data = new byte[1024];
        int len = 0;
        FileOutputStream fileOutputStream = null;
        try {
            String fileName = new Date().getTime() + ".png";
            // 创建保存文件夹
            savePath = savePath + fileName;
            //照片存放地址D:\nginx\www\movies\face
            filePath = filePath + fileName;
            fileOutputStream = new FileOutputStream(filePath);
            while ((len = inputStream.read(data)) != -1) {
                fileOutputStream.write(data, 0, len);

            }
            return savePath;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "";
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    }
}
